<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>迭代器</title>
  </head>
  <body>
    <script>
      function ajax(url) {
        var xmlhttp; // 创建XMLHttpRequest对象
        if (window.XMLHttpRequest) {
          xmlhttp = new XMLHttpRequest();
        } else {
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // 兼容早期浏览器
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send();
        xmlhttp.onreadystatechange = function () {
          if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
            var obj = JSON.parse(xmlhttp.responseText);
            getData.next(obj); // 将数据传入next方法
          }
        };
      }
      function* gen() {
        yield ajax("static/a.json");
        yield ajax("static/b.json");
        yield ajax("static/c.json");
      }
      let getData = gen();
      getData.next(); // 开始执行第一个ajax请求
    </script>
  </body>
</html>
